/*
 * @Author: your name
 * @Date: 2021-05-09 18:44:50
 * @LastEditTime: 2021-05-12 13:10:25
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \after-end\app\service\routes.js
 */
const Service = require('egg').Service;
const { SuccessModel, FailModel } = require('../resModel');
const { filterRoutes, filterIpId, filterRouter } = require('../utils/filterRoutes');
class RoutesService extends Service {

    //后台管理查找路由
    async fandRoutes() {
        const { ctx, app } = this;
        const { "0": { userId } } = ctx.state;
        const data = await app.mysql.query(`select * from routesTable`);
        const findData = await app.mysql.query(`select * from userTable where userId=?`, [userId]);

        if (findData[0].router) {
            return await new SuccessModel(filterRouter(JSON.parse(findData[0].router)), "Success");
        } else {
            return await new SuccessModel(filterRouter(data), "Success");
        }
    }


    async updateRoutes() {
        const { ctx, app } = this;
        const { id, children } = ctx.request.body;
        console.log(id, children);
        const row = {
            id: id,
            router: JSON.stringify(children),
        };

        const data = await this.app.mysql.update('userTable', row)

        return data;
    }
}

module.exports = RoutesService;